package HomeWork;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ext {
    public static class Connect {
        static Connect cm;
        Connection con;
        static String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3308/cw?&useSSL=false&serverTimezone=Asia/Shanghai";
        String user = "root";
        String password = "root";

        static {
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException("创建失败!");
            }
        }

        public static Connect getInstance() {
            if (cm == null) {
                synchronized (Connect.class) {
                    if (cm == null) {
                        cm = new Connect();
                    }
                }
            }
            return cm;
        }

        public Connection getConnection() {
            if (con == null) {
                try {
                    con =  DriverManager.getConnection(url,user,password);
                } catch (Exception e) {
                    throw new RuntimeException("连接失败!");
                }
            }
            return con;
        }

        public static void Close(ResultSet rs, Statement sta, Connection con) {
            if (rs != null) {
                try {
                    if (rs != null)
                        rs.close();
                    if (sta != null)
                        sta.close();
                    if (con != null)
                        con.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
}
